﻿<script language="jscript" runat="server">
function ActionCryptoJS(){
	F.require("CryptoJS").globalize__("CryptoJS");
	this.empty=function(a){
		F.echo("没找到动作：" + a);
	};
}
ActionCryptoJS.prototype=new IAction(); //继承IAction对象，下面才可以直接用this.assign，this.display
ActionCryptoJS.prototype.Index=function(){
	
};
/*
 *摘要算法：MD5/sha[1|3|224|256|384|512]/pbkdf2/evpkdf/ripemd160
 */
ActionCryptoJS.prototype.md5=function(){
	CryptoJS.require.MD5();
	F.echo(CryptoJS.MD5("admin"),true);
}
ActionCryptoJS.prototype.hmacmd5=function(){
	CryptoJS.require.Hmac().MD5();
	F.echo(CryptoJS.HmacMD5("admin","12212"),true);
}
ActionCryptoJS.prototype.hmacmd52=function(){
	//CryptoJS.library="rollups";
	CryptoJS.require.Hmacs.MD5("rollups");
	CryptoJS.require.Hmacs.SHA1("rollups");
	F.echo(CryptoJS.HmacMD5("admin","12212"),true);
	F.echo(CryptoJS.HmacSHA1("admin","12212"),true);
}
ActionCryptoJS.prototype.sha1=function(){
	CryptoJS.require.SHA1();
	F.echo(CryptoJS.SHA1("admin"),true);
}
ActionCryptoJS.prototype.sha3=function(){
	CryptoJS.require("sha3","x64-core");
	//等效于CryptoJS.require.X64().SHA3();
	//当使用快捷调用时，必要的加载项已经自动加载，这里直接用CryptoJS.require.SHA3()就可以了
	F.echo(CryptoJS.SHA3("admin"),true);
}
ActionCryptoJS.prototype.sha224=function(){
	CryptoJS.require("sha224","sha256");
	F.echo(CryptoJS.SHA224("admin"),true);
}
ActionCryptoJS.prototype.sha256=function(){
	CryptoJS.require("sha256");
	F.echo(CryptoJS.SHA256("admin"),true);
}
ActionCryptoJS.prototype.sha384=function(){
	CryptoJS.require("sha384","x64-core","sha512");
	F.echo(CryptoJS.SHA384("admin"),true);
}
ActionCryptoJS.prototype.sha512=function(){
	CryptoJS.require("sha512","x64-core");
	F.echo(CryptoJS.SHA512("admin"),true);
}
ActionCryptoJS.prototype.pbkdf2=function(){
	CryptoJS.require.PBKDF2();
	var salt=CryptoJS.enc.Utf8.parse("salt");
	var mi=CryptoJS.PBKDF2("你好，欢迎使用，这是一个PBKDF2测试", salt, {keySize: 128/32,iterations: 10});
	F.echo(mi,true);
}
ActionCryptoJS.prototype.evpkdf=function(){
	CryptoJS.require.EvpKDF();
	var salt=CryptoJS.enc.Utf8.parse("salt");
	var mi=CryptoJS.EvpKDF("你好，欢迎使用，这是一个evpkdf测试", salt, {keySize: 128/32,iterations: 10});
	F.echo(mi,true);
}
ActionCryptoJS.prototype.ripemd160=function(){
	CryptoJS.require("ripemd160");
	var mi=CryptoJS.RIPEMD160("你好，欢迎使用，这是一个RIPEMD160测试");
	F.echo(mi,true);
}

/*
 *编码算法：base64/utf8/utf16/hex/latin1
 */
ActionCryptoJS.prototype.base64=function(){
	CryptoJS.require("enc-base64");
	var src = CryptoJS.enc.Utf8.parse("测试123456");
	var base64string = CryptoJS.enc.Base64.stringify(src);
	F.echo(base64string,true);
	src = CryptoJS.enc.Base64.parse(base64string);
	F.echo(CryptoJS.enc.Utf8.stringify(src));
}

ActionCryptoJS.prototype.utf8=function(){
	var src = CryptoJS.enc.Utf8.parse("测试123456");
	F.echo(src,true);
	F.echo(CryptoJS.enc.Utf8.stringify(src));
}
ActionCryptoJS.prototype.hex=function(){
	var src = CryptoJS.enc.Hex.parse("48656c6c6f2c20576f726c6421");
	F.echo(src,true);
	F.echo(CryptoJS.enc.Utf8.stringify(src));
}
ActionCryptoJS.prototype.latin1=function(){
	var src = CryptoJS.enc.Latin1.parse("f2c20576f726c64");
	F.echo(src,true);
	F.echo(CryptoJS.enc.Latin1.stringify(src));
}
ActionCryptoJS.prototype.utf16=function(){
	CryptoJS.require("enc-utf16");
	var src = CryptoJS.enc.Utf16.parse("测试123456");
	F.echo(src,true);
	F.echo(CryptoJS.enc.Utf16.stringify(src));
}

/*
 *加密算法：rc4/aes
 */
ActionCryptoJS.prototype.rc4=function(){
	CryptoJS.require.Format.Hex();
	CryptoJS.require.RC4();
	var src = CryptoJS.enc.Utf8.parse("测试123456");
	var key = CryptoJS.enc.Utf8.parse("D6BCB11B0F091963"); 
	var encrypted = CryptoJS.RC4.encrypt(src,key);
	F.echo(encrypted.toString(CryptoJS.format.Hex),true);
	src = CryptoJS.RC4.decrypt(encrypted,key);
	F.echo(CryptoJS.enc.Utf8.stringify(src));
}
ActionCryptoJS.prototype.rabbitlegacy = function(){
	CryptoJS.require.Padding();
	CryptoJS.require.RabbitLegacy();
	var srcs = CryptoJS.enc.Utf8.parse("测试rabbit加解密");
	var key = CryptoJS.enc.Utf8.parse("CB49164C6490EDAB");   
	var iv  = CryptoJS.enc.Utf8.parse('D6BCB11B0F0919EF');
	var cfg={ 
    	iv: iv,
    	mode:CryptoJS.mode.CBC,
    	padding:CryptoJS.pad.ZeroPadding
    };
	var encrypted = CryptoJS.RabbitLegacy.encrypt(srcs, key,cfg); //加密
	srcs = CryptoJS.RabbitLegacy.decrypt(encrypted, key,cfg); //解密
	encrypted = encrypted.ciphertext.toString(CryptoJS.Hex);
	srcs = srcs.toString(CryptoJS.enc.Utf8)
	F.echo("加密结果："+encrypted,true);
	F.echo("解密结果："+srcs,true);
};
ActionCryptoJS.prototype.rabbit = function(){
	CryptoJS.require.Padding();
	CryptoJS.require.Rabbit();
	var srcs = CryptoJS.enc.Utf8.parse("测试rabbit加解密");
	var key = CryptoJS.enc.Utf8.parse("CB49164C6490EDAB");   
	var iv  = CryptoJS.enc.Utf8.parse('D6BCB11B0F091963');
	var cfg={ 
    	iv: iv,
    	mode:CryptoJS.mode.CBC,
    	padding:CryptoJS.pad.ZeroPadding
    };
	var encrypted = CryptoJS.Rabbit.encrypt(srcs, key,cfg); //加密
	var encryptedtext = encrypted.ciphertext.toString(CryptoJS.Hex);
	srcs = CryptoJS.Rabbit.decrypt(CryptoJS.lib.CipherParams.create({ ciphertext:encrypted.ciphertext}), CryptoJS.enc.Utf8.parse("CB49164C6490EDAB"),cfg); //解密
	F.echo("加密结果："+encryptedtext,true);
	srcs = srcs.toString(CryptoJS.enc.Utf8)
	F.echo("解密结果："+srcs,true);
};
ActionCryptoJS.prototype.des=function(){
	CryptoJS.require.DES().Padding().Format.Hex();
	var srcs = "测试des加解密";
	var key = CryptoJS.enc.Utf8.parse("CB49164C6490EDAB");   
	var iv  = CryptoJS.enc.Utf8.parse('D6BCB11B0F091963');
	var cfg={ 
    	iv: iv,
    	mode:CryptoJS.mode.CBC,
    	padding:CryptoJS.pad.ZeroPadding,
    	format:CryptoJS.format.Hex
    };
	var encrypted = CryptoJS.DES.encrypt(srcs, key, cfg); //加密
	encrypted = encrypted.toString();
	srcs = CryptoJS.DES.decrypt(encrypted, key, cfg); //解密
	srcs = CryptoJS.enc.Utf8.stringify(srcs);
	F.echo(encrypted,true);
	F.echo(srcs,true);
};
ActionCryptoJS.prototype.aes=function(){
	CryptoJS.require.AES().Mode().Padding().Format.Hex();
	var srcs = "测试aes加解密";
	var key = CryptoJS.enc.Utf8.parse("CB49164C6490EDAB");   
	var iv  = CryptoJS.enc.Utf8.parse('D6BCB11B0F091963');

	//Modes:CFB/CTR/CTRGladman/ECB/OFB
	//CryptoJS.require("mode");

	//Paddings:AnsiX923/Iso10126/Iso97971/NoPadding/ZeroPadding
	//CryptoJS.require("pad");
	var cfg={ 
    	iv: iv,
    	mode:CryptoJS.mode.CBC,
    	padding:CryptoJS.pad.ZeroPadding,
    	format:CryptoJS.format.Hex
    };
	var encrypted = CryptoJS.AES.encrypt(srcs, key, cfg); //加密
	encrypted = encrypted.toString();
	srcs = CryptoJS.AES.decrypt(encrypted,key, cfg); //解密
	srcs = CryptoJS.enc.Utf8.stringify(srcs);
	F.echo(encrypted,true);
	F.echo(srcs,true);
};
</script>